package com.whw.dubbo.provider.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.whw.dubbo.provider.service.common.FileUtil;
import com.whw.dubbo.provider.service.common.IpHelper;
import com.whw.dubbo.provider.service.common.IpRelation;

@RestController
@RequestMapping("/ip")
public class IpController {
	@RequestMapping("ip")
	public String ip(String ip){
		 long startTime = System.currentTimeMillis();
		String result=IpHelper.findRegionByIp(ip);
		System.out.println("耗时 : " + (System.currentTimeMillis() - startTime));
		System.out.println("全球".equals(result));
		return result;
	}
	
	@RequestMapping("test")
	public Map<String, Object> test() throws Exception{
		String file =  IpController.class.getClassLoader().getResource("keys.txt").getFile();
        BufferedReader ipRelationReader = FileUtil.readFile(file);
        String line;
        List<String> list = new ArrayList<String>();
        List<Map<String, String>> regionsIp = new ArrayList<Map<String, String>>();
        List<Map<String, String>> regionsIp2 = new ArrayList<Map<String, String>>();
        Map<String, Object>data=new HashMap<String, Object>();
        while((line = ipRelationReader.readLine()) != null){
           //System.out.println(line);
           //System.out.println(line.substring(line.indexOf("key")+4,line.length()-1));
            //list.add(ipRelation);
           list.add(line.substring(line.indexOf("key")+4,line.length()-1));
        }
        for (String ip : list) {
        	String result=IpHelper.findRegionByIp(ip);
        	if("全球".equals(result)){
        		Map<String, String> map=new HashMap<String, String>();
            	map.put(ip, result);
            	regionsIp.add(map);
        	}else{
        		Map<String, String> map=new HashMap<String, String>();
            	map.put(ip, result);
            	regionsIp2.add(map);
        	}
        	
		}
        data.put("regionsIp", regionsIp);
        data.put("regionsIp2", regionsIp2);
		return data;
	}
}
